import { stringify } from 'qs';
import _ from 'lodash';
import request, { checkResultCode, checkStatus } from '../utils/request';
import { Constants } from '../utils';
import { ipConfig } from './config';

// http://mesdev.chalco-steering.com:1708/appgateway/api-ms/pub/todo/search?appId=APP_MES&status=2&page=9&rows=10&type=1

// export const serverName = '/api-pea-device-170236';
export const serverName = ipConfig.baseServerName();

const imageUrl = `${serverName}/logistics/fileController/download/`; // 图片路径

const checkExistUrl = `${serverName}/checkCord/checkExist`; // 查询是否有巡检任务
const queryCheckEquipmentUrl = `${serverName}/device/equipment/queryCheckEquipment`; // 巡检首页设备查询接口
const queryCheckTaskUrl = `${serverName}/checkCord/queryCheckTask`; // 巡检任务查询接口
const findByEquipmentCodeUrl = `${serverName}/checkSpot/findByEquipmentCode`; // 设备扫描查询设备巡检项
const addEntryUrl = `${serverName}/equiprecord/addEntry`; // 巡检添加设备接口
const getSpotRecordByEquipCheckIdUrl = `${serverName}/device/spotRecord/getSpotRecordByEquipCheckId`; // 巡检设备明细接口


export const checkExist = async () => {
  return request(`${ipConfig.url(checkExistUrl)}`, {
    method: 'GET',
  }).then(mapCheckExist);
};

export const queryCheckEquipment = async (params) => {
  const defaultParams = { rows: Constants.PAGE_SIZE, page: 1 };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(queryCheckEquipmentUrl)}?${stringify(newParams)}`, {
    method: 'GET',
  }).then(mapQueryCheckEquipment);
};

export const queryCheckTask = async (params) => {
  const defaultParams = { rows: Constants.PAGE_SIZE, page: 1 };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(queryCheckTaskUrl)}?${stringify(newParams)}`, {
    method: 'GET',
  }).then(mapQueryCheckTask);
};

export const findByEquipmentCode = async (params) => {
  return request(`${ipConfig.url(findByEquipmentCodeUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(mapFindByEquipmentCode);
};

export const addEntry = async params => {
  return request(`${ipConfig.url(addEntryUrl)}`, {
    method: 'POST',
    body: params,
  }).then(response => response.data);
};

export const getSpotRecordByEquipCheckId = async (params) => {
  return request(`${ipConfig.url(getSpotRecordByEquipCheckIdUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(response => response.data);
};

const mapCheckExist = response => {
  return {
    data: response.data,
  };
};

const mapQueryCheckEquipment = response => {
  // `http://gateway.pea.cs2025.cn/gateway/api-pea-device/logistics/fileController/download/2c94b38a673f928801673fe2b5250011?token=${global.__TOKEN_ACCESS__}`
  const data = _.map(response.data, it => ({
    _id: it.failureId,
    createTime: it.createTime,
    equipcheckId: it.equipcheckId,
    equipmentCode: it.equipmentCode,
    equipmentName: it.equipmentName,
    equipmentId: it.equipmentId,
    equipmentStatusName: it.equipmentStatusName,
    monthCheckNum: it.monthCheckNum,
    result: it.result,
    resultName: it.resultName,
    status: it.status,
    failurePic: it.showImage ? `${ipConfig.url(imageUrl)}${it.showImage}?token=${global.__TOKEN_ACCESS__}` : 'no-pic',
  }));

  return {
    list: data,
    page: response.pageNum,
    total: response.totalNum,
  };
};

const mapQueryCheckTask = response => {
  // `http://gateway.pea.cs2025.cn/gateway/api-pea-device/logistics/fileController/download/2c94b38a673f928801673fe2b5250011?token=${global.__TOKEN_ACCESS__}`
  // const data = _.map(response.data, it => ({
  //   _id: it.failureId,
  //   createTime: it.createTime,
  //   equipcheckId: it.equipcheckId,
  //   equipmentCode: it.equipmentCode,
  //   equipmentName: it.equipmentName,
  //   equipmentId: it.equipmentId,
  //   equipmentStatusName: it.equipmentStatusName,
  //   monthCheckNum: it.monthCheckNum,
  //   result: it.result,
  //   resultName: it.resultName,
  //   status: it.status,
  //   failurePic: it.showImage ? `${ipConfig.url(imageUrl)}${it.showImage}?token=${global.__TOKEN_ACCESS__}` : 'no-pic',
  // }));
  return {
    list: response.data,
    page: response.pageNum,
    total: response.totalNum,
  };
};

const mapFindByEquipmentCode = response => {
  // `http://gateway.pea.cs2025.cn/gateway/api-pea-device/logistics/fileController/download/2c94b38a673f928801673fe2b5250011?token=${global.__TOKEN_ACCESS__}`
  const data = _.map(response.data.checkspotList, it => ({
    checkWay: [],
    inspection: it.inspection,
    inspectionType: it.inspectionType,
    normalLowerLimit: it.normalLowerLimit,
    normalUpperLimit: it.normalUpperLimit,
    objType: it.objType,
    resultItemsDic: it.resultItemsDic,
    resultItemsDicitem: it.resultItemsDicitem,
    resultType: it.resultType,
    splitItemDescription: it.splitItemDescription,
    splitItemId: it.splitItemId,
    spotId: it.spotId,
    warmLowerLimit: it.warmLowerLimit,
    warmUpperLimit: it.warmUpperLimit,
    checked: true,
    choice: true,
    desc: '',
  }));

  return {
    data,
    equipmentId: response.data.equipmentId,
    equipmentName: response.data.equipmentName,
  };
};
